package com.gopay.common.cps.service.sms.impl;

import com.gopay.common.constants.sms.SmsMassEnum;
import com.gopay.common.cps.dao.sms.SmsMassContentDAO;
import com.gopay.common.cps.dao.sms.SmsMassDAO;

import com.gopay.common.cps.service.sms.SmsMassBaseService;
import com.gopay.common.domain.sms.SmsMass;
import com.gopay.common.domain.sms.SmsMassContent;
import com.gopay.common.util.FastJsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SmsMassBaseServiceImpl implements SmsMassBaseService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SmsMassDAO smsMassDAO;
    @Autowired
    private SmsMassContentDAO smsMassContentDAO;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    public SmsMassContent batchInsertSms(List<SmsMass> sms,SmsMassContent smsMassContent) {
        logger.info("短信新增操作:短信批量插入DB,执行方法batchInsertSms[{}]----start",smsMassContent.getSmsId());
        SmsMassContent massContent = null;
       /* try {*/
            smsMassContent.setCreatTs(new Date());
            smsMassContent.setLastUpdTs(smsMassContent.getCreatTs());
            smsMassContent.setVersion(0);
            logger.info("短信新增操作:短信批量插入DB,保存smsMassContent[{}]----start",FastJsonUtil.toJSONString(smsMassContent));
            massContent = smsMassContentDAO.save(smsMassContent);
            logger.info("短信新增操作:短信批量插入DB,保存smsMassContent[{}]----end", FastJsonUtil.toJSONString(massContent));
            bathInsertSmsMass(sms);
       /* } catch (Exception e) {
            logger.error("短信新增操作:短信批量插入DB,执行方法batchInsertSms异常----铛铛铛",e);
            SmsMassContent content = smsMassContentDAO.selectMass(massContent.getSmsId());
            if(content!=null){
                logger.error("短信新增操作:短信批量插入DB,执行方法batchInsertSms异常---将smsMassContent更新为失效状态start");
                smsMassContentDAO.updateMassSatus(SmsMassEnum.SEND_CANCLE.getValue(),massContent.getSmsId());
                logger.error("短信新增操作:短信批量插入DB,执行方法batchInsertSms异常---将smsMassContent更新为失效状态end");
            }else {
                logger.error("短信新增操作:短信批量插入DB,执行方法batchInsertSms异常---SmsMassContent未入库,异常处理逻辑结束");
            }
        }*/
        logger.info("短信新增操作:短信批量插入DB,执行方法batchInsertSms[{}]----end",smsMassContent.getSmsId());
        return massContent;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    public void bathInsertSmsMass(List<SmsMass> sms) {
        logger.info("短信批量插入DB,保存SmsMass[{}]----start",FastJsonUtil.toJSONString(sms));
        smsMassDAO.bathInsertSmsMass(sms);
        logger.info("短信批量插入DB,保存SmsMass[{}]----end", FastJsonUtil.toJSONString(sms));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    public void batchDelete(String key) {
        Map<String,Object> map = new HashMap<>();
        map.put("smsId",key);
        map.put("lastUpdTs",new Date());
        smsMassContentDAO.updateByHql("update SmsMassContent set status='2',lastUpdTs=:lastUpdTs where smsId=:smsId",map);
        smsMassDAO.deleteByHql("delete from SmsMass where smsContentId='" + key + "'");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    public SmsMassContent batchUpdateSms(List<SmsMass> smsMassList, SmsMassContent smsMassContent) {
        logger.info("短信更新操作:短信批量插入DB,执行方法batchUpdateSms[{}]----start",FastJsonUtil.toJSONString(smsMassContent));
        if(smsMassList!=null && smsMassList.size()>0) {
            logger.info("短信更新操作:执行方法batchUpdateSms----删除SmsMass[执行sql:delete from SmsMass where smsContentId={}]开始",smsMassContent.getSmsId());
            smsMassDAO.deleteByHql("delete from SmsMass where smsContentId='" + smsMassContent.getSmsId() + "'");
            logger.info("短信更新操作:执行方法batchUpdateSms----删除SmsMass[执行sql:delete from SmsMass where smsContentId={}]完毕",smsMassContent.getSmsId());
            smsMassDAO.bathInsertSmsMass(smsMassList);
        }
        smsMassContent.setLastUpdTs(new Date());
        smsMassContentDAO.update(smsMassContent);
        logger.info("短信更新操作:短信批量插入DB,执行方法batchUpdateSms[{}]----end",FastJsonUtil.toJSONString(smsMassContent));
        return smsMassContent;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    public String updateBatchByKey(List<SmsMass> kes,String status) {
        if(kes ==null || kes.size()==0) {
            return null;
        }
        String packageId = smsMassContentDAO.getNextSq();
        for (SmsMass smsMass : kes){
            StringBuffer sql = new StringBuffer();
            sql.append(" UPDATE SMS_MASS_DETAIL SET SEND_STATUS=:status , LAST_UPD_TS=:lastUpdTs , PACKAGE_ID=:packageId WHERE SMS_MASS_ID=:smsId");
            Map<String,Object> map = new HashMap<>();
            map.put("smsId",smsMass.getSmsId());
            map.put("packageId",packageId);
            map.put("status",status);
            map.put("lastUpdTs",new Date());
            smsMassDAO.updateBySql(sql.toString(),map);
        }
        return packageId;
    }

    @Override
    public void updateSmsStatusByPackgeId(String status, String pakgeId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" UPDATE SMS_MASS_DETAIL SET SEND_STATUS=:status , LAST_UPD_TS=:lastUpdTs  WHERE PACKAGE_ID=:packageId");
        Map<String,Object> map = new HashMap<>();
        map.put("packageId",pakgeId);
        map.put("status",status);
        map.put("lastUpdTs",new Date());
        smsMassDAO.updateBySql(sql.toString(),map);
    }

    @Override
    public void updateSmsAndContentToFail(String status, String pakgeId, String smsId) {
        updateSmsStatusByPackgeId("4",pakgeId);
    }

    @Override
    public String getContentSeq() {
        String nextSq = smsMassContentDAO.getNextSq();
        logger.info("短信群发获取SmsMassContent序列值为[{}]",nextSq);
        return nextSq;
    }

    @Override
    public void updateMassSatus(String status, String smsId) {
        smsMassContentDAO.updateMassSatus(status,smsId);
    }

    @Override
    public List<SmsMassContent> selectTheSmsCompensate() {
        return smsMassContentDAO.selectTheSmsCompensate();
    }

    @Override
    public List<SmsMassContent> selectTheSmsWillBeSendByPage(int pageNum, int pagesizeContent) {
        return  smsMassContentDAO.selectTheSmsWillBeSendByPage(pageNum,pagesizeContent);
    }
}
